/// 积分变更历史模型
class PointsHistoryModel {
  /// 变更ID
  final int? id;
  
  /// 用户ID
  final int userId;
  
  /// 积分变化量（正数表示增加，负数表示减少）
  final int changeAmount;
  
  /// 变更原因/描述
  final String reason;
  
  /// 变更时间
  final String changeTime;
  
  /// 变更类型（充值、消费、系统奖励等）
  final String? changeType;
  
  /// 变更后余额
  final int? balanceAfter;
  
  PointsHistoryModel({
    this.id,
    required this.userId,
    required this.changeAmount,
    required this.reason,
    required this.changeTime,
    this.changeType,
    this.balanceAfter,
  });
  
  factory PointsHistoryModel.fromJson(Map<String, dynamic> json) {
    return PointsHistoryModel(
      id: json['id'],
      userId: json['user_id'] ?? json['userid'] ?? 0,
      changeAmount: json['points_change'] ?? json['change_amount'] ?? json['change'] ?? 0,
      reason: json['change_reason'] ?? json['reason'] ?? json['description'] ?? '',
      changeTime: json['created_at'] ?? json['change_time'] ?? '',
      changeType: json['change_type']?.toString(),
      balanceAfter: json['points_after'] ?? json['balance_after'] ?? json['balance'],
    );
  }
  
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'user_id': userId,
      'change_amount': changeAmount,
      'reason': reason,
      'change_time': changeTime,
      'change_type': changeType,
      'balance_after': balanceAfter,
    };
  }
  
  /// 判断是否为增加积分
  bool get isIncrease => changeAmount > 0;
  
  /// 判断是否为减少积分
  bool get isDecrease => changeAmount < 0;
  
  /// 获取格式化的积分变化文本
  String get formattedChangeAmount {
    if (changeAmount > 0) {
      return '+$changeAmount积分';
    } else if (changeAmount < 0) {
      return '$changeAmount积分';
    } else {
      return '0积分';
    }
  }
}

/// 积分历史响应模型
class PointsHistoryResponse {
  final String status;
  final String message;
  final List<PointsHistoryModel> history;
  final int? totalCount;
  final int? currentPage;
  final int? totalPages;
  
  PointsHistoryResponse({
    required this.status,
    required this.message,
    required this.history,
    this.totalCount,
    this.currentPage,
    this.totalPages,
  });
  
  factory PointsHistoryResponse.fromJson(Map<String, dynamic> json) {
    // 处理嵌套的 data 结构
    List<PointsHistoryModel> historyList = [];
    int? total;
    int? page;
    int? totalPages;
    
    if (json['data'] is Map<String, dynamic>) {
      // 后端返回格式: {status, data: {total, page, records: [...]}}
      final dataMap = json['data'] as Map<String, dynamic>;
      historyList = (dataMap['records'] as List<dynamic>?)
          ?.map((item) => PointsHistoryModel.fromJson(item))
          .toList() ?? [];
      total = dataMap['total'];
      page = dataMap['page'];
      totalPages = dataMap['total_pages'];
    } else if (json['data'] is List) {
      // 如果直接是数组
      historyList = (json['data'] as List<dynamic>?)
          ?.map((item) => PointsHistoryModel.fromJson(item))
          .toList() ?? [];
    } else if (json['history'] != null) {
      // 或者使用 history 字段
      historyList = (json['history'] as List<dynamic>?)
          ?.map((item) => PointsHistoryModel.fromJson(item))
          .toList() ?? [];
    }
    
    return PointsHistoryResponse(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '',
      history: historyList,
      totalCount: total ?? json['total_count'],
      currentPage: page ?? json['current_page'],
      totalPages: totalPages ?? json['total_pages'],
    );
  }
  
  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      'history': history.map((h) => h.toJson()).toList(),
      'total_count': totalCount,
      'current_page': currentPage,
      'total_pages': totalPages,
    };
  }
}

